//
//  STMenuTitleView.swift
//  SwiftDemo
//
//  Created by Steven on 2020/8/3.
//  Copyright © 2020 Steven. All rights reserved.
//

import UIKit
import Foundation

protocol DXMenuMoveProtocol:class {
    func menuMoveProtocol(fromIndex:NSInteger,toIndex:NSInteger)
}

enum DXIndicatorType:NSInteger {
    case DXIndicatorTypeDefault = 0,//默认与按钮长度相同
        DXIndicatorTypeEqualTitile,//与文字长度相同
        DXIndicatorTypeCustom,//自定义文字边缘延伸宽度
        DXIndicatorTypeNone
}//指示器类型枚举

class DXMenuTitleView: UIView {
    
    var indicatoreType:DXIndicatorType = .DXIndicatorTypeDefault{
        didSet{
            moveIndicatorView()
        }
    }
    var titleArr: Array = [String]()
    var menuArr: Array = [UIButton]()
    var menuWidth:CGFloat = 60.0

    var selectedIndex:NSInteger = 0{
        didSet{
            moveIndicatorView()
        }
    }
    
    var indicatorHeight:CGFloat = 2
    var indicatorExtension:CGFloat = 0 {
        didSet{
            moveIndicatorView()
        }
    }
    
    weak var menuMoveDelegate:DXMenuMoveProtocol?
    //按钮间距离
    var menuMargin:CGFloat = 20.0
    //字体大小
    var menuFont:CGFloat = 14.0
    
     lazy var menuScrollView: UIScrollView = {
        let menuScrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.dx_width, height: self.dx_height))
        menuScrollView.showsHorizontalScrollIndicator  = false
        menuScrollView.contentSize = CGSize(width: self.dx_width, height: self.dx_height)
        return menuScrollView
    }()
    lazy var indicatorView:UIView = {
       let view = UIView()
        view.backgroundColor = KFont_blueColor
        menuScrollView.addSubview(view)
        return view
    }()
    
    var menuScrollCount:CGFloat = 5.0{
        didSet{
            menuScrollView.contentSize = CGSize(width: CGFloat(menuScrollCount)*menuWidth, height: dx_height)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience  init(frame: CGRect,titleArr:[String]) {
        self.init(frame:frame)
        self.backgroundColor = .white
        setupMenu(titleArr: titleArr)
        addSubview(menuScrollView)
        
        let view:UIView = {
            let view = UIView()
            self.addSubview(view)
            view.snp.makeConstraints { (make) in
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(0.5)
                make.bottom.equalTo(self)
            }
            return view
        }()
        view.backgroundColor = KLineColor

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        moveIndicatorView()
    }
    func moveIndicatorView()
    {
        if menuArr.isEmpty {
            return
        }
        let selectedBtn = menuArr[selectedIndex]
        let indicatorW:CGFloat = (selectedBtn.titleLabel?.text?.dx_strWidth(fontSize: menuFont))!

        UIView.animate(withDuration: 0.05, animations: {
            switch self.indicatoreType{
            case .DXIndicatorTypeDefault:
                  self.indicatorView.frame = CGRect(x: selectedBtn.dx_x, y: selectedBtn.dx_bottomY-self.indicatorHeight, width: selectedBtn.dx_width, height: self.indicatorHeight)
            case .DXIndicatorTypeEqualTitile:
                self.indicatorView.center = CGPoint(x: selectedBtn.dx_centerX, y: selectedBtn.dx_bottomY-self.indicatorHeight/2)
                   self.indicatorView.bounds = CGRect(x: 0, y: 0,width:indicatorW , height: self.indicatorHeight)
            case .DXIndicatorTypeCustom:
                self.indicatorView.center = CGPoint(x: selectedBtn.dx_centerX, y: selectedBtn.dx_bottomY-self.indicatorHeight/2)
                               self.indicatorView.bounds = CGRect(x: 0, y: 0,width:self.indicatorExtension , height: self.indicatorHeight)
            case .DXIndicatorTypeNone:
                self.indicatorView.frame = CGRect(x: 0, y: 0, width: 0, height: 0);

            }
            
            
        }) { (finished) in
            
        }
    }
    func setupMenu(titleArr:[String]){
        let _ = menuArr.map { (make) -> Void in
            make.removeFromSuperview()
        }
        
        menuArr.removeAll()
        if titleArr.isEmpty{
            return
        }
        
        var totalMenuWidth:CGFloat = 0
        for (_,val) in titleArr.enumerated() {
            let menuW = val.dx_strWidth(fontSize: menuFont) + menuMargin
            let menu = UIButton(type: UIButton.ButtonType.custom)
            menu.frame = CGRect(x: totalMenuWidth, y: 0.0, width: menuW, height: dx_height)
            menu.setTitle(val, for: UIControl.State.normal)
            menu.setTitleColor(.black, for: .normal)
            menu.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            menu.addTarget(self, action: #selector(menuAction), for: .touchUpInside)
            menuScrollView.addSubview(menu)
            menuArr.append(menu)
            totalMenuWidth = totalMenuWidth + menuW
        }
        menuScrollView.contentSize.width = totalMenuWidth
    }
    @objc func menuAction(sender:UIButton)  {
        let index = menuArr.firstIndex(of: sender)
        if selectedIndex == index {
            return
        }
        if menuMoveDelegate != nil {
            menuMoveDelegate?.menuMoveProtocol(fromIndex: selectedIndex, toIndex: index!)
        }
        
        selectedIndex = index!
        moveIndicatorView()
    }
}
